package com.changpay.zywy.ui.activity.count

import android.support.v7.widget.GridLayoutManager
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.ProducePresentAdapter
import com.changpay.zywy.app.App.Companion.context
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.bean.TotalActivationBean
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.wight.SpaceItemDecoration
import com.changpay.zywy.wight.WheelCircleView
import kotlinx.android.synthetic.main.title_right_layout.*
import kotlinx.android.synthetic.main.activity_count_action_total.*

class CountActionTotalNewActivity : BaseActivity(), IView {

    var wheelView: WheelCircleView? = null
    var tvTotal: TextView? = null
    var tvacttion: TextView? = null
    var tvunacttion: TextView? = null
    override fun showLoading() {
    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
    }

    override fun showMsg(msg: String) {
    }

    override fun showError(errorMsg: String) {
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_count_action_total
    }

    override fun initData() {
    }

    override fun initView() {
        tv_title.text = "激活率"
        iv_back.setOnClickListener { finish() }
        initRecyclerView()
    }

    override fun start() {
        loadData()
    }

    private fun loadData() {
        // 加载底部数据
        val params = HashMap<String, String>()
        params["client"] = "android"
        params["key"] = token
        params["gc_id"] = intent.getStringExtra("gc_id")
        datas.clear()
        RetrofitHelper.service.get_total_activation(params).sss(this) {
            val datas = it.result.activation
            tvTotal!!.text = it.result.activation_total.stock_num.toString() + "台"
            tvunacttion!!.text = it.result.activation_total.un_activation_num.toString() + "台"
            tvacttion!!.text = it.result.activation_total.activation_num.toString() + "台"

            for (i in datas.indices) {
                datas[i].type = i % 4
                datas[i].total = it.result.activation_total.stock_num
            }

            mAdapter.addData(it.result.activation)
            android.os.Handler().postDelayed({
                wheelView?.run { setDataPer(it.result.activation_total.activation_num, it.result.activation_total.stock_num) }
            }, 100)
        }
    }


    private var headerView: View? = null
    /**
     * datas
     */
    private val datas = mutableListOf<TotalActivationBean.ActivationBean>()

    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        this.let {
            SpaceItemDecoration(context!!)
        }
    }

    /**
     * Adapter
     */
    private val mAdapter: ProducePresentAdapter by lazy {
        ProducePresentAdapter(context, datas)
    }


    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: GridLayoutManager by lazy {
        GridLayoutManager(context, 2)
    }


    /**
     * ItemClickListener
     */
    private val ItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->

    }


    /**
     * ItemChildClickListener
     */
    private val onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { _, view, position ->


    }

    private fun initRecyclerView() {
        recyclerView_total.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
//            itemAnimator = DefaultItemAnimator()
//            recyclerViewItemDecoration.let { addItemDecoration(it) }
        }
        mAdapter.run {
            bindToRecyclerView(recyclerView_total)
            onItemChildClickListener = this@CountActionTotalNewActivity.onItemChildClickListener
            headerView = layoutInflater.inflate(R.layout.rv_totoal_header, null)
            tvTotal = headerView?.run { this.findViewById<TextView>(R.id.tv_total) }
            wheelView = headerView?.run { this.findViewById<WheelCircleView>(R.id.ct_action_all) }
            tvacttion = headerView?.run { this.findViewById<TextView>(R.id.tv_action_act) }
            tvunacttion = headerView?.run { this.findViewById<TextView>(R.id.tv_action_unact) }
            headerView?.run {
                layoutParams =
                    ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            }
            mAdapter.addHeaderView(headerView)
//            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
            onItemClickListener = this@CountActionTotalNewActivity.ItemClickListener
        }
    }

}